﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace bollar
{
    class Ball
    {
        //Traits of the ball
        private Vector2 _position; //(x,y)
        private Vector2 _velocity; //Velocity is positive if ball is going down, negative if up.
        private Vector2 _acceleration;

        private float _radius;
        private Texture2D ballImg;

        public float Restitution { get; set; } //Vad gör detta?

        public Ball(Vector2 spawnPosition, Vector2 velocity, float radius)
        {
            _position = spawnPosition;
            _velocity = velocity;
            _acceleration = Vector2.Zero;
            _radius = radius;

            Restitution = 1f; //Vad gör detta?
        }

        public void AddAcceleration(Vector2 vect)
        {
            _acceleration += vect;
        }

        #region UPDATE
        Boolean bounce = true;
        /* Update ball's traits
         * Collision handling
         */
        public void Update(float timeStep)
        {
            _velocity += timeStep * _acceleration;
            _position += timeStep * _velocity;
            _acceleration = Vector2.Zero;

            /*Collision*/ 
            if (_position.Y > 600 - _radius) //South
            {
                    Vector2 normal = new Vector2(0, -1);
                    DotCollision(normal, _position.Y - 600 + _radius);
            }

            if (_position.Y < 0 + _radius)
            {
                Vector2 normal = new Vector2(0, 1);
                DotCollision(normal, _radius - _position.Y);
            }

            if (_position.X > 800 - _radius) //East
            {
                Vector2 normal = new Vector2(-1, 0);
                DotCollision(normal, _position.X -800 + _radius);
            }

            if (_position.X < 0 + _radius) //West
            {
                Vector2 normal = new Vector2(1, 0);
                DotCollision(normal, _radius - _position.X);
            }

            //Bara fulgrejer under ;D
            Util.Debug.Shape.DrawCircleLocal(600, 600, 100, Color.Red);
            Vector2 vec = _position - new Vector2(600, 600);
            float r = 100f;

            float radSq = r + _radius;
            radSq *= radSq;
            if (vec.LengthSquared() < radSq)
            {
                vec.Normalize();
                DotCollision(vec);
            }
        }

/*
       private void DotCollision(Vector2 normal)
        {
            float dot = Vector2.Dot(normal, _velocity); //Längden av velocity projektion på normalen. |v_n| = Dot(n,v)

            if (dot < 0) //Om bollen är påväg mot en kant
            {
                Vector2 v_n = dot * normal;
                _velocity -= (1.0f + Restitution) * v_n;
                bounce = true;
            }
            else
                bounce = false;
        }
*/
        private void DotCollision(Vector2 normal, float penetration)
        {
            float dot = Vector2.Dot(normal, _velocity); //Längden av velocity projektion på normalen. |v_n| = Dot(n,v)

            if (dot < 0) //Om bollen är påväg mot en kant
            {
                Vector2 v_n = dot * normal;
                _velocity -= (1.0f + Restitution) * v_n;
            }

            if (penetration > 0)
                _position += normal * penetration * 0.5f;
        }

        #endregion UPDATE

        public void LoadContent(ContentManager Content)
        {
            ballImg = Content.Load<Texture2D>("ball");
        }

        //batch kan vara onödig, men bra att ha om du vill rita en sprite till bollen.
        public void Draw(SpriteBatch batch)
        {
           // batch.Draw(ballImg, new Vector2(_position.X + _radius, _position.Y + _radius), Color.White);            
            batch.Draw(ballImg, _position, null, Color.White, 0f, new Vector2(ballImg.Width * 0.5f, ballImg.Height * 0.5f), 1.0f*(_radius/10), SpriteEffects.None, 0f); //Scale is divided by 10 because radius of original ball = 10.
            Util.Debug.Shape.DrawCircleLocal(_position.X, _position.Y, _radius, Color.Red);
        }
    }
}
